/**
 * 
 */
package ua.at.tsvetkov.samizdat.data;

import java.io.File;

import ua.at.tsvetkov.samizdat.Const;
import android.app.Activity;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class DataLoadedAuthors {

	public LoadedAuthorBooks[]	authorsData;
	private Activity	activity;

	/**
	 * Загружает данные по загруженным авторам
	 */
	public DataLoadedAuthors(Activity activity) {
		this.activity = activity;
	}

	/**
	 * Загрузка данных
	 */
	public void load() {
		File dirs = new File(Const.PATH_BOOKS);
		String[] paths = dirs.list();
		authorsData = new LoadedAuthorBooks[paths.length];
		int emptyDirCount = 0;
		for (int i = 0; i < paths.length; i++) {
			String path = Const.PATH_BOOKS + paths[i];
			File dir = new File(path);
			if (dir.isDirectory()) {
				String[] files = dir.list();
				String[] books = new String[0];
				int hiddenCount = 0;
				for (int j = 0; j < files.length; j++) {
					if (files[j].startsWith(".")) {
						hiddenCount++;
					}
				}
				books = new String[files.length - hiddenCount];
				int k = 0;
				for (int j = 0; j < files.length; j++) {
					if (!files[j].startsWith(".")) {
						books[k] = files[j];
						k++;
					}
				}
				for (int j = 0; j < books.length; j++) {
					books[j] = path + File.separator + books[j];
				}
				if (books.length > 0) {
					authorsData[i] = new LoadedAuthorBooks(paths[i], books);
				} else {
					emptyDirCount++;
				}
			}
		}
		if (emptyDirCount > 0) {
			LoadedAuthorBooks[] authorsDataTmp = new LoadedAuthorBooks[authorsData.length - emptyDirCount];
			int k = 0;
			for (int i = 0; i < authorsData.length; i++) {
				if (authorsData[i] != null) {
					authorsDataTmp[k] = authorsData[i];
					k++;
				}
			}
			authorsData = authorsDataTmp;
		}
	}

	/**
	 * Загрузка данных избранных авторов
	 */
	public void loadFavorite() {
		File dirs = new File(Const.PATH_BOOKS);
		FavoriteAuthors favoriteAuthors = new FavoriteAuthors(activity);
		favoriteAuthors.load();
		String[] authorsTmp = dirs.list();
		int count = 0;
		for (String author : favoriteAuthors.data) {
			for (int i = 0; i < authorsTmp.length; i++) {
				if (authorsTmp[i].contains(author)) {
					count++;
				}
			}
		}
		authorsData = new  LoadedAuthorBooks[count];
		String[] authors = new String[count];
		count = 0;
		for (String author : favoriteAuthors.data) {
			for (int i = 0; i < authorsTmp.length; i++) {
				if (authorsTmp[i].contains(author)) {
					authors[count++] = authorsTmp[i];
				}
			}
		}
		
		int emptyDirCount = 0;
		for (int i = 0; i < authors.length; i++) {
			String path = Const.PATH_BOOKS + authors[i];
			File dir = new File(path);
			if (dir.isDirectory()) {
				String[] files = dir.list();
				String[] books = new String[0];
				int hiddenCount = 0;
				for (int j = 0; j < files.length; j++) {
					if (files[j].startsWith(".")) {
						hiddenCount++;
					}
				}
				books = new String[files.length - hiddenCount];
				int k = 0;
				for (int j = 0; j < files.length; j++) {
					if (!files[j].startsWith(".")) {
						books[k] = files[j];
						k++;
					}
				}
				for (int j = 0; j < books.length; j++) {
					books[j] = path + File.separator + books[j];
				}
				if (books.length > 0) {
					authorsData[i] = new LoadedAuthorBooks(authors[i], books);
				} else {
					emptyDirCount++;
				}
			}
		}
		if (emptyDirCount > 0) {
			LoadedAuthorBooks[] authorsDataTmp = new LoadedAuthorBooks[authorsData.length - emptyDirCount];
			int k = 0;
			for (int i = 0; i < authorsData.length; i++) {
				if (authorsData[i] != null) {
					authorsDataTmp[k] = authorsData[i];
					k++;
				}
			}
			authorsData = authorsDataTmp;
		}
	}

	public void deleteAutor(int pos) {
		String path = Const.PATH_BOOKS + authorsData[pos].author;
		File dir = new File(path);
		String[] files = dir.list();
		for (int i = 0; i < files.length; i++) {
			File file = new File(path + File.separator +files[i]);
			file.delete();
		}
		dir.delete();
	}
}
